<?php

namespace app\admin\model;

use think\Model;

class QuestionsList extends Model
{
    public static $typeList = [
        1 => ['title' => '单选题', 'class' => 'layui-bg-orange', 'type' => 'radio', 'icon' => 'radio-field'],
        2 => ['title' => '多选题', 'class' => 'layui-bg-green', 'type' => 'multiple', 'icon' => 'multiple-field'],
        3 => ['title' => '判断题', 'class' => 'layui-bg-cyan', 'type' => 'jude', 'icon' => 'jude-field'],
    ];

    protected $autoWriteTimestamp = true;

    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
        if (input('get.cid')) $where[] = ['a.cid', '=', input('get.cid')];
        $list = self::alias('a')
            ->leftJoin('questions_category b', 'a.cid = b.id')
            ->order('a.id', 'desc')
            ->where($where)
            ->field('a.*, b.title as category_title')
            ->paginate($limit);
        foreach ($list as $k => $v) {
            $list[$k]['category_title'] = $v['category_title'] ?? '分类已删';
            $list[$k]['type_title'] = self::$typeList[$v['type']]['title'] ?? '类型已删';
            $list[$k]['type_class'] = self::$typeList[$v['type']]['class'] ?? '';
        }
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public function answers()
    {
        return $this->hasMany(QuestionsAnswers::class, 'qid', 'id');
    }

    public static function dealAnswers($answers)
    {
        if (empty($answers)) return [];
        $hasCorrect = [];
        $result = [];
        foreach ($answers as $k => $v) {
            if ($v['correct']) $hasCorrect[] = chr(65 + $k);
            $result[] = [
                'value' => chr(65 + $k),
                'label' => $v['title']
            ];
        }
        if (!$hasCorrect) return [];
        return [$hasCorrect, $result];
    }

    public static function getQuestionsList($categoryIds, $number)
    {
        $list = self::whereIn('cid', $categoryIds)
            ->where('status', 1)
            ->with(['answers'])
            ->limit($number)
            ->orderRaw('rand()')
            ->select()
            ->toArray();
        $data = [];
        foreach ($list as $k => $v) {
            $answers = self::dealAnswers($v['answers']);
            if (!$answers) continue;
            $data[] = [
                'type' => QuestionsList::$typeList[$v['type']]['type'],
                'icon' => QuestionsList::$typeList[$v['type']]['icon'],
                'options' => [
                    'topic' => $v['title'],
                    'correct' => $answers[0],
                    'errorRes' => '',
                    'optionItems' => $answers[1]
                ]
            ];
        }
        return $data;
    }

    public static function getQuestionsBySearch($keyword)
    {
        $list = self::where('title', 'like', '%' . $keyword . '%')
            ->where('status', 1)
            ->field('id, title')
            ->select()
            ->toArray();
        return ['msg' => '', 'code' => 200, 'data' => $list];
    }
}